package com.dstz.demo.utils;

import com.dstz.base.core.util.StringUtil;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.util.UUID;

public class Marydon {
//    /**
//     * 图片转base64字符串
//     * @param path
//     * @return
//     */
//    public static String PictoBase64(String path) {
//        InputStream in = null;
//        byte[] data = null;
//        // 读取图片字节数组
//        try {
//            in = new FileInputStream(path);
//            data = new byte[in.available()];
//            in.read(data);
//            in.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        // 对字节数组Base64编码
//        BASE64Encoder encoder = new BASE64Encoder();
//        return encoder.encode(data);// 返回Base64编码过的字节数组字符串
//    }
//    public static boolean Base64toPic(String imgStr) {
//
//        // 对字节数组字符串进行Base64解码并生成图片
//        if (imgStr == null) // 图像数据为空
//            return false;
//        BASE64Decoder decoder = new BASE64Decoder();
//        try {
//            // Base64解码
//            byte[] b = decoder.decodeBuffer(imgStr);
//            for (int i = 0; i < b.length; ++i) {
//                if (b[i] < 0) {// 调整异常数据
//                    b[i] += 256;
//                }
//            }
//            String imgName = UUID.randomUUID().toString().replace("-", "");
//            String imgType = ".png";
//            // 生成png图片
//            String imgFilePath = "C:/Users/Administrator/Desktop/" + imgName + imgType;// 新生成的图片
//            OutputStream out = new FileOutputStream(imgFilePath);
//            out.write(b);
//            out.flush();
//            out.close();
//            return true;
//        } catch (Exception e) {
//            return false;
//        }
//    }

    public static boolean Base64ToImage(String imgStr,String imgFilePath) { // 对字节数组字符串进行Base64解码并生成图片

        if (StringUtil.isEmpty(imgStr)) // 图像数据为空
            return false;

        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }

            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();

            return true;
        } catch (Exception e) {
            return false;
        }

    }
    public static void main(String[] args) {
        String str ="/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAoHBwgHBgoICAgLCgoLDhgQDg0NDh0VFhEYIx8lJCIfIiEmKzcvJik0KSEiMEExNDk7P" +
                "j4+JS5ESUM8SDc9Pjv/2wBDAQoLCw4NDhwQEBw7KCIoOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozv/wAARCABkAGQDASIAAhEBAxE" +
                "B/8QAHAABAAIDAQEBAAAAAAAAAAAAAAEDAgQIBQYH/8QAPBAAAgADAwgIBAILAAAAAAAAAAECAxEEElEUISIxVJGS8AUHExVSYdHSBnGBwXKzFyMyMzdBVnOio+L/xAAZAQEBAQEBAQAAAAAAAAAA" +
                "AAAAAgEEBQP/xAAdEQEBAAMBAAMBAAAAAAAAAAAAARESUQIEEyFB/9oADAMBAAIRAxEAPwD9ku80F3mgvIXkAu80F3mgvIXkAu80F3mgvIXkAu80F3mgvIXkAu80F3mgvIXkAu80FEmvMXkKpteQE0WCAq" +
                "sUAOb5fWH8XUhcXTto14Q+h+1dX3SFs6W+DLHbLfaI59omRTL0yLW6RtLV5HN1knQRTM+pfzZ0T1XRX/gKwtar81f7Iic216PyJ4+mXz19bdXnvYovPeySG0mk2qvUsSnnF1ee9kPM4aVzuj3GREWuH5/YCQ" +
                "AAAAAAAAABxvGlBE1BHeWKOkup/P1bdHfjm/mRHNZ0p1P/AMNujvxzfzIhR9ua86yqbPlzL8Su1qkzYAAxi/ag+f2ZkYxa4fn9gMgAAAAAAAAAB8d+jj4Q/pWDi/7Pe6MsFm6GsEuwdHdGR2azS23B" +
                "LgcNFV1euLFnpgxWZxrdrN2WdxQ+4drN2WdxQ+42QDM41u1m7LO4ofcO1m1rks76uH3GyAZnGv287ZZu+D3Dt52yzd8HuNgAzONft52yzd8HuHbztlm74PcbABmca/bztlm74PcZQTonGoY5UctxZob1M7+jZcU" +
                "z/wB9Z/7jf+MQJi/xbnxW4Eg1KjJLPs0ngQySz7NJ4EXVWKFVijMRW16pySz7NJ4EMks+zSeBF1VihVYoYhteqcks+zSeBDJLPs0ngRdVYoVWKGIbXqnJLPs0ngQySz7NJ4EXVWKFVihiG16pySz7NJ4EMks+zSeBF1V" +
                "ihVYoYhteqcks+zSeBGUEmVKf6uVBA3mrDCkWVWKK4JagjiamRR3471Ioq3c2pYI3DNr1Z9QSAxjpcsaXLJqsUKrFARpcsaXLJqsUKrFARpcsaXLJqsUKrFARpcsaXLJqsUKrFARpcsaXLJqsUKrFARpcsZ6qr1k1WKIedqmI" +
                "E0eLBIA8bvaf4Je5+o72n+CXufqARl06zh3taPBL3P1M5PSc+ZOggcMF" +
                "IoknSuPzAGS+ZxdbrdNs09QQQwtOGulX1Nbva0eCXufqALf1PmTB3taPBL3P1He1o8Evc/UAZVrOHe1o8Evc/U9CxT4rTI7SNJO9TNUA2I9ySNinz3hrmoBT4vIj6VnwxxQqCXmbWp+oAIy6dZx//9k=";
        String imgFilePath="D:\\Marydon\\d8247d8ca92c218800b2b8b1ffab7f5.jpeg";
        Base64ToImage(str,imgFilePath);
//        System.out.println(Base64toPic(str));
//        str = PictoBase64("D:\\Marydon\\d8247d8ca92c218800b2b8b1ffab7f5.jpeg");
//        System.out.println(str);
    }
}
